home *** CD-ROM | disk | FTP | other *** search
/ Mac Mania 2 / MacMania 2.toast / Demo's / Music⁄Sounds / BeepSay 1.0.1 / init.c < prev    next >
Encoding:
Text File  |  1993-06-03  |  5.7 KB  |  242 lines  |  [TEXT/MPS ]

  1. /******************************************************************************
  2. **
  3. **  Folder Name:    BS
  4. **     File Name:    INIT.c
  5. **
  6. **   Copyright:    © 1993 Siren Enterprises, all rights reserved.
  7. **
  8. **   Description:    Lil´ BeepSay - loads resident code, shows icons.
  9. **
  10. *******************************************************************************
  11. **                       A U T H O R   I D E N T I T Y
  12. *******************************************************************************
  13. **
  14. **    Initials    Name
  15. **    --------    -----------------------------------------------
  16. **    kw            Ken Wieschhoff
  17. **    DS            Dave Spotts
  18. **
  19. *******************************************************************************
  20. **                      R E V I S I O N   H I S T O R Y
  21. *******************************************************************************
  22. **
  23. **      Date        Time    Author    Description
  24. **    --------    -----    ------    ---------------------------------------------
  25. **    05/17/93    15:30    KW        Original version
  26. **
  27. ******************************************************************************/
  28.  
  29. //  Make sure the compiler only generates 68000 code.  We'll catch missing 
  30. // components below.
  31. #pragma     processor 68000
  32.  
  33. #include    <OSUtils.h>
  34. #include    <Files.h>
  35. #include    <Types.h>
  36. #include    <Resources.h>
  37. #include    <PLStringFuncs.h>
  38. #include    <Notification.h>
  39. #include    <Packages.h>
  40. #include    "ShowINIT.h"
  41. #include    "Resident.h"
  42. #include    "Preferences.h"
  43. #include    "Utilities.h"
  44.  
  45. //
  46. // System Traps enhanced.
  47. //
  48. #define        cGestaltTrapNum                0xAD
  49. #define        cHWPrivTrapNum                0xA198
  50. #define        cUnImplTrapNum                0x9F
  51.  
  52. // ResIDs of ICN# and cicn resources to indicate status
  53.  
  54. #define        cShowInitSucc            0
  55. #define        cShowInitGesNotImp        1
  56. #define        cShowInitFCBErr            2
  57. #define        cShowResNotloaded        3
  58. #define        cShowInitResNF            4
  59.  
  60. TrapType GetTrapType(short theTrap);
  61.  
  62. // ResidentMain function 'prototype'
  63. typedef pascal OSErr (*mainFuncPtr)(StringPtr fileName, short volume, long dirID);
  64.  
  65.  
  66.  
  67. entrypoint()
  68. {
  69.     Boolean            GestaltIsImplemented;
  70.     OSErr            err;
  71.     Handle            ourHandle;
  72.     FCBPBRec        theParmBlock;
  73.     Str255            theName;
  74.     short            theRefNum;
  75.     short            iconID = cShowInitSucc;
  76.     long            dummy;
  77.     PreferencesHdl    thePreferences = nil;
  78.  
  79.     // Don't load w/o Gestalt 
  80.     GestaltIsImplemented = (NGetTrapAddress(cGestaltTrapNum,GetTrapType( cGestaltTrapNum)) !=
  81.                               NGetTrapAddress(cUnImplTrapNum, GetTrapType( cUnImplTrapNum)));
  82.     if (!(GestaltIsImplemented)) {
  83.         iconID = cShowInitGesNotImp;
  84.         goto exit;
  85.     }
  86.  
  87.     // Get ahold of ourselves.
  88.     ourHandle = RecoverHandle((Ptr) entrypoint);
  89.  
  90.     // turn it into a resRefNum
  91.     theRefNum = HomeResFile(ourHandle);    
  92.     
  93.     theParmBlock.ioRefNum = theRefNum;
  94.     theParmBlock.ioFCBIndx = (long) 0;
  95.     theParmBlock.ioNamePtr = &theName;
  96.  
  97.     //    Find our file name, volume, and dirID
  98.     err = PBGetFCBInfo(&theParmBlock, false);
  99.     if ( err != noErr) {
  100.         iconID = cShowInitFCBErr;
  101.         goto exit;
  102.     }
  103.     
  104.     
  105.  
  106.     // Checked all we can check, load/detach/call the resident code.
  107.     {
  108.         Handle    residentCode;
  109.         mainFuncPtr    codeMain;
  110.         THz    saveZone;
  111.  
  112.         
  113.         // Force the code to load in the system heap AND force the code's globals,
  114.         // segments, etc. to go there too!  It remembers the 'zone' it loads in
  115.         // and makes it the current zone for all subsequent work it has to do.
  116.         saveZone = GetZone();
  117.         SetZone( SystemZone() );
  118.  
  119.         residentCode = Get1Resource( 'SYSB', 1);
  120.         if ( residentCode == nil) {
  121.             iconID = cShowInitResNF;
  122.             SetZone( saveZone);
  123.             goto exit;
  124.         }
  125.         
  126.         MoveHHi( residentCode);
  127.         HLock( residentCode);            // Should be already, but doesn't hurt
  128.         DetachResource( residentCode);
  129.         codeMain = ( mainFuncPtr ) (*residentCode);
  130.         
  131.         // Call the main routine to set trap, add Gestalt, add Apple Event support, etc.
  132.         err = (*codeMain)( &theName[0], theParmBlock.ioFCBVRefNum, theParmBlock.ioFCBParID);
  133.         
  134.         SetZone( saveZone);
  135.  
  136.         
  137.     }
  138.  
  139.     if ( err ) {
  140.         iconID = cShowResNotloaded;
  141.         goto exit;
  142.     }
  143.     
  144.     // Read the preferences from disk
  145.     err = _ReadPreferences( &thePreferences, kBeepSayPrefs);
  146.     if (( err ) || !(**thePreferences).iconAtStartup)
  147.         iconID = -1;
  148.     
  149.  
  150. exit:
  151.  
  152.     if ( thePreferences != nil)
  153.         DisposeHandle( (Handle) thePreferences);
  154.  
  155.     if ( iconID >= 0) {
  156.         ShowINIT( iconID, -1);            
  157.         Delay( 60L, &dummy);        // Wait a second…
  158.     }
  159.  
  160. }
  161.  
  162.  
  163. TrapType GetTrapType(short theTrap)
  164.  
  165. {
  166.     // OS traps start with A0, Tool with A8 or AA. 
  167.     if ((theTrap & 0x0800) == 0)                // per D.A.
  168.         return OSTrap;
  169.     else
  170.         return ToolTrap;
  171. } // GetTrapType 
  172.  
  173. pascal void _DisposeError( NMRec *theNMRec)
  174. {
  175.     OSErr    err;
  176.  
  177.     err = NMRemove( theNMRec);
  178.  
  179.     DisposePtr( (Ptr) theNMRec->nmStr );
  180.     DisposePtr( (Ptr) theNMRec );
  181. }
  182.  
  183. pascal void DisposeError( NMRec *theNMRec)
  184. {
  185.     long    hostA5;
  186.     Handle    ourA5 = *OldSysBeep();
  187.     
  188.     hostA5 = GetA5();
  189.  
  190.     
  191.     SetA5((long)StripAddress((*ourA5)) + A5Size() - 32);
  192.  
  193.         _DisposeError( theNMRec);
  194.         
  195.     RestoreA5World( hostA5);
  196. }
  197.  
  198. pascal void ShowStr( StringPtr str)
  199. {
  200.     NMRec*        nmPtr;
  201.     StringPtr    text;
  202.     
  203.     // Use the Notification Manager to report errors.
  204.     nmPtr = (NMRec*) NewPtr( sizeof( NMRec) );
  205.     if ( nmPtr == nil) {
  206.         return;
  207.     }
  208.     text = (StringPtr) NewPtr( str[0] + 1);
  209.     if ( text == nil) {
  210.         DisposePtr( (Ptr) nmPtr);
  211.         return;
  212.     }
  213.     PLstrcpy( text, str);
  214.     
  215.     nmPtr->qType     = 8;        
  216.     nmPtr->nmMark     = 0;                        // no mark in application menu
  217.     nmPtr->nmIcon     = nil;                        // nil = no icon in menubar or a handle to SICN resource
  218.     nmPtr->nmSound    = (Handle)-1;                // pick a sound, use -1 for System Beep or nil for no sound.
  219.     nmPtr->nmStr     = text;                        // display this string in dialog
  220.     nmPtr->nmResp     = (NMProcPtr)DisposeError;    // Routine to dispose of the pointer block. (nil - Use default)
  221.     nmPtr->nmRefCon = 0L;                        // no refcon
  222.  
  223.     if( NMInstall( nmPtr) != noErr) {
  224.         DisposePtr( (Ptr) nmPtr);
  225.         DisposePtr( (Ptr) text);
  226.     }
  227. }
  228.  
  229.  
  230. pascal void ShowStrNum( StringPtr str, OSErr err)
  231. {
  232.     Str255    errText, strText;
  233.     
  234.     NumToString( (long) err, errText);
  235.     
  236.     PLstrcpy(strText, str);
  237.     PLstrcat(strText,"\p Error = ");
  238.     PLstrcat(strText, errText);
  239.     ShowStr( strText);
  240. }
  241.  
  242.